package com.shangyueshang.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.shangyueshang.utils.DateUtil;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author JokeyFeng
 * @date: 2023/8/5 10:45
 * @project: shangyueshang
 * @package: com.shangyueshang.converter
 * @comment:
 */
public class LocalDateConverter implements Converter<LocalDate> {
	
	@Override
	public Class supportJavaTypeKey() {
		return LocalDate.class;
	}
	
	@Override
	public CellDataTypeEnum supportExcelTypeKey() {
		return CellDataTypeEnum.STRING;
	}
	
	@Override
	public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
									   GlobalConfiguration globalConfiguration) throws ParseException {
		if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
			return LocalDate.parse(cellData.getStringValue());
		}
		return LocalDate.parse(
				cellData.getStringValue(),
				DateTimeFormatter.ofPattern(
						contentProperty.getDateTimeFormatProperty()
								.getFormat()
				)
		);
	}
	
	@Override
	public CellData convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
									   GlobalConfiguration globalConfiguration) {
		return new CellData(value.toString());
	}
}
